package com.sca.point.controller;

import com.sca.point.entity.api.BaseResult;
import com.sca.point.entity.enums.ResultEnum;
import com.sca.point.service.PointService;
import com.sca.point.utils.RedisBloomFilter;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * @author liuxianfei
 * @since 2020/11/13 16:24
 */
@RestController
@RequestMapping("/point/v1")
public class PointController {

    private final PointService pointService;
    private final RedisBloomFilter redisBloomFilter;

    public PointController(PointService pointService, RedisBloomFilter redisBloomFilter) {
        this.pointService = pointService;
        this.redisBloomFilter = redisBloomFilter;
    }

    @PostMapping("/add")
    @ApiOperation("增加积分")
    public BaseResult<?> add(@RequestHeader("tx_xid") String xid, Integer userId, Integer point) {
        System.out.println("point xid : " + xid);
        if (pointService.addPoint(userId, point)) {
            return new BaseResult<>();
        }
        return new BaseResult<>(ResultEnum.ERR);
    }

    @GetMapping("/bf/add")
    public BaseResult<?> bfPut(Integer uid) {
        redisBloomFilter.put(String.valueOf(uid));
        return new BaseResult<>();
    }

    @GetMapping("/bf")
    public BaseResult<?> bfFlag(Integer uid) {
        return new BaseResult<>(redisBloomFilter.mightContain(String.valueOf(uid)));
    }

}
